package edu.fatec.veterinaria.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import edu.fatec.veterinaria.model.Consulta;

public class ConsultaDAO implements IConsultaDAO {

	Connection conexao = GenericDao.getConnection();


	/* (non-Javadoc)
	 * @see edu.fatec.veterinaria.dao.IConsultaDAO#insereConsulta(edu.fatec.veterinaria.model.Consulta)
	 */
	@Override
	public boolean insereConsulta(Consulta consulta) {
		try{
			PreparedStatement ps0 = conexao.prepareStatement("insert into  Consulta" +
			" values (?,?,?,?,?,?,?)");
			ps0.setString(1, consulta.getDescricao());
			ps0.setDate(2, consulta.getData());
			ps0.setString(3, consulta.getHorario());
			ps0.setDouble(4, consulta.getValor());
			ps0.setInt(5, consulta.getCodAnimal());
			ps0.setInt(6, consulta.getCodVeterinario());
			ps0.setString(7, consulta.getCodCliente());
			ps0.execute();
			ps0.close();

			return true;

		}catch (SQLException e){
			e.printStackTrace();
			return false;
		}


	}

	/* (non-Javadoc)
	 * @see edu.fatec.veterinaria.dao.IConsultaDAO#pesquisaConsultas(java.lang.String)
	 */
	@Override
	public List<Consulta> pesquisaConsultas(String cpf){
		List<Consulta> listaConsultas = new ArrayList<Consulta>();
		ResultSet rs = null;
		try{
			PreparedStatement ps0 = conexao.prepareStatement("select * from Consulta" +
			" where codCliente = ?");
			ps0.setString(1, cpf);
			ps0.execute();
			rs = ps0.getResultSet();
			while(rs.next()){
				Consulta consulta = new Consulta();
				consulta.setCodConsulta(rs.getInt("codigo"));
				consulta.setDescricao(rs.getString("descricao"));
				consulta.setData(rs.getDate("dataConsulta"));
				consulta.setHorario(rs.getString("horaConsulta"));
				consulta.setValor(rs.getDouble("valor"));
				consulta.setCodAnimal(rs.getInt("codAnimal"));
				consulta.setCodVeterinario(rs.getInt("codVeterinario"));
				consulta.setCodCliente(rs.getString("codCliente"));
				listaConsultas.add(consulta);

			}
			ps0.close();


		}catch (SQLException e){
			e.printStackTrace();

		}

		return listaConsultas;
	}

	/* (non-Javadoc)
	 * @see edu.fatec.veterinaria.dao.IConsultaDAO#deletaConsulta(int)
	 */
	@Override
	public boolean deletaConsulta(int codConsulta){

		try{
			PreparedStatement ps0 = conexao.prepareStatement("delete from Consulta" +
			" where codigo = ?");
			ps0.setInt(1, codConsulta);
			ps0.execute();
			ps0.close();
			return true;
		}catch (SQLException e){
			e.printStackTrace();
			return false;
		}
	}

	/* (non-Javadoc)
	 * @see edu.fatec.veterinaria.dao.IConsultaDAO#pesquisaConsultasPorData(java.lang.String)
	 */
	@Override
	public List<Consulta>pesquisaConsultasPorData(String data){
		List<Consulta> listaConsultas = new ArrayList<Consulta>();
		ResultSet rs = null;
		try{
			PreparedStatement ps0 = conexao.prepareStatement("select * from Consulta" +
			" where dataConsulta = ?");
			ps0.setString(1, data);
			ps0.execute();
			rs = ps0.getResultSet();
			while(rs.next()){
				Consulta consulta = new Consulta();
				consulta.setCodConsulta(rs.getInt("codigo"));
				consulta.setDescricao(rs.getString("descricao"));
				consulta.setData(rs.getDate("dataConsulta"));
				consulta.setHorario(rs.getString("horaConsulta"));
				consulta.setValor(rs.getDouble("valor"));
				consulta.setCodAnimal(rs.getInt("codAnimal"));
				consulta.setCodVeterinario(rs.getInt("codVeterinario"));
				consulta.setCodCliente(rs.getString("codCliente"));
				listaConsultas.add(consulta);

			}
			ps0.close();


		}catch (SQLException e){
			e.printStackTrace();

		}

		return listaConsultas;
	}

	/* (non-Javadoc)
	 * @see edu.fatec.veterinaria.dao.IConsultaDAO#pesquisarConsulta(int, java.lang.String)
	 */
	@Override
	public boolean pesquisarConsulta(int i, String codCliente) {
		try{
			Connection conexao = GenericDao.getConnection();
			String sql = "Select * from Consulta " +
			"where codCliente like ? and codAnimal = ?";
			PreparedStatement prepare = conexao.prepareStatement(sql);
			prepare.setString(1, codCliente);
			prepare.setInt(2, i);
			prepare.execute();
			ResultSet result = prepare.getResultSet();
			if (result.next()){
				return true;
			}else{
				return false;
			}

		}catch (SQLException e) {
			return false;
		}

	}


	/* (non-Javadoc)
	 * @see edu.fatec.veterinaria.dao.IConsultaDAO#excluirConsultas(java.lang.String, int)
	 */
	@Override
	public void excluirConsultas(String codCliente, int codigoAnimal) {
		try{
			Connection conexao = GenericDao.getConnection();
			String sql = "delete Consulta " +
			"where codCliente like ? and codAnimal = ?";
			PreparedStatement prepare = conexao.prepareStatement(sql);
			prepare.setString(1, codCliente);
			prepare.setInt(2, codigoAnimal);
			prepare.execute();
			prepare.close();

		}catch(SQLException s){
			s.printStackTrace();
		}

	}

	/* (non-Javadoc)
	 * @see edu.fatec.veterinaria.dao.IConsultaDAO#excluirConsultas(java.lang.String)
	 */
	@Override
	public void excluirConsultas(String codCliente) {
		try{
			Connection conexao = GenericDao.getConnection();
			String sql = "delete Consulta " +
			"where codCliente like ?";
			PreparedStatement prepare = conexao.prepareStatement(sql);
			prepare.setString(1, codCliente);
			prepare.execute();
			prepare.close();

		}catch(SQLException s){
			s.printStackTrace();
		}
		
	}
	
	


}
